import { defineConfig } from 'wxt'
import { resolve } from 'node:path'
import { readFileSync } from 'node:fs'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// See https://wxt.dev/api/config.html
export default defineConfig({
  modules: ['@wxt-dev/module-vue'],
  outDir: resolve(__dirname, '../.output'),
  extensionApi: 'chrome',
  manifest: {
    name: '工具集合',
    description: "",
    permissions: ['tabs', 'userScripts', 'storage', 'scripting'],
    host_permissions: ['*://*.chrome.com/*', '*://*.douyin.com/*'],
    content_security_policy: {
      extension_pages: "script-src 'self' 'wasm-unsafe-eval'; object-src 'self';",
      sandbox:
        "script-src 'self' 'unsafe-inline' 'unsafe-eval'; sandbox allow-scripts allow-forms allow-popups allow-modals; child-src 'self';",
    },
  },
  vite() {
    return {
      plugins: [AutoImport({ resolvers: [ElementPlusResolver()] }), Components({ resolvers: [ElementPlusResolver()] })],
    }
  },
})

function getHostPermissionsFromPackageJSON(): string[] {
  try {
    const text = readFileSync(resolve(__dirname, 'package.json'), { encoding: 'utf-8' })
    const { host_permissions } = JSON.parse(text).manifest
    return Array.isArray(host_permissions) ? host_permissions : []
  } catch (error) {
    return []
  }
}
